package com.ruoyi.system.service.impl.config;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson2.JSON;
import com.alibaba.fastjson2.JSONObject;
import com.ruoyi.system.domain.video.VsLang;
import com.ruoyi.system.service.video.IVsLangService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.system.mapper.config.VsDramasConfigMapper;
import com.ruoyi.system.domain.config.VsDramasConfig;
import com.ruoyi.system.service.config.IVsDramasConfigService;

/**
 * 配置Service业务层处理
 * 
 * @author zzx
 * @date 2024-05-21
 */
@Service
public class VsDramasConfigServiceImpl implements IVsDramasConfigService 
{
    @Autowired
    private VsDramasConfigMapper vsDramasConfigMapper;

    @Autowired
    private IVsLangService vsLangService;

    /**
     * 查询配置
     * 
     * @param id 配置主键
     * @return 配置
     */
    @Override
    public VsDramasConfig selectVsDramasConfigById(Integer id)
    {
        return vsDramasConfigMapper.selectVsDramasConfigById(id);
    }

    /**
     * 查询配置列表
     * 
     * @param vsDramasConfig 配置
     * @return 配置
     */
    @Override
    public List<VsDramasConfig> selectVsDramasConfigList(VsDramasConfig vsDramasConfig)
    {
        return vsDramasConfigMapper.selectVsDramasConfigList(vsDramasConfig);
    }

    /**
     * 新增配置
     * 
     * @param vsDramasConfig 配置
     * @return 结果
     */
    @Override
    public int insertVsDramasConfig(VsDramasConfig vsDramasConfig)
    {
        return vsDramasConfigMapper.insertVsDramasConfig(vsDramasConfig);
    }

    /**
     * 修改配置
     * 
     * @param vsDramasConfig 配置
     * @return 结果
     */
    @Override
    public int updateVsDramasConfig(VsDramasConfig vsDramasConfig)
    {
        return vsDramasConfigMapper.updateVsDramasConfig(vsDramasConfig);
    }

    /**
     * 批量删除配置
     * 
     * @param ids 需要删除的配置主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasConfigByIds(Integer[] ids)
    {
        return vsDramasConfigMapper.deleteVsDramasConfigByIds(ids);
    }

    /**
     * 删除配置信息
     * 
     * @param id 配置主键
     * @return 结果
     */
    @Override
    public int deleteVsDramasConfigById(Integer id)
    {
        return vsDramasConfigMapper.deleteVsDramasConfigById(id);
    }

    @Override
    public Map phoneInit(VsDramasConfig vsDramasConfig) {
        List<VsDramasConfig> list = vsDramasConfigMapper.selectVsDramasConfigList(vsDramasConfig);
        VsLang vsLang = vsLangService.selectVsLangById(vsDramasConfig.getLangId());
        Map<String,Map<String,Object>> result = new HashMap<>();
        Map<String, Object> mapsList = new HashMap<>();
        for (VsDramasConfig c: list) {

            if("dramas".equals(c.getName())) {
                Map<String,Object> map = JSON.parseObject(c.getValue());

                for (Map.Entry<String, Object> entry : map.entrySet()) {
                    Object childObject = entry.getValue();

                    // 检查childObject是否是Map类型
                    if (childObject instanceof Map<?, ?>) {
                        @SuppressWarnings("unchecked") // 假设我们知道它是Map<String, String>类型
                        Map<String, String> childMap = (Map<String, String>) childObject;

                        // 检查是否存在commonKey，如果存在则添加到新的子级Map中（这里需要强制转换，因为父级Map是<String, Object>）
                        if (childMap.containsKey(vsLang.getLang())) {
                            entry.setValue((Object) childMap.get(vsLang.getLang()));
                        }

                    }
                }
                map.put("lang",vsLang.getLang());
                result.put("system",map);
            }
            if ("share".equals(c.getName())) {
                Map<String,Object> map = JSON.parseObject(c.getValue());
                result.put("share",map);
            }
            if ("paypal".equals(c.getName())) {
                Map<String,Object> map = new HashMap<>();
                map.put("name","Paypal支付");
                map.put("type","paypal");
                map.put("logo","https://video.nymaite.cn/assets/addons/dramas/img/user_wallet_apply/paypal.png");
                mapsList.put("paypal",map);
            }
            if ("stripe".equals(c.getName())) {
                Map<String,Object> map = new HashMap<>();
                map.put("name","Stripe支付");
                map.put("type","stripe");
                map.put("logo","https://video.nymaite.cn/assets/addons/dramas/img/user_wallet_apply/stripe.png");
                mapsList.put("stripe",map);
            }
            if (mapsList.size() > 0) {
                result.put("payment", mapsList);
            }
            if ("google".equals(c.getName())) {
                Map<String,Object> map = new HashMap<>();
                Map<String,Object> jsonObject = JSON.parseObject(c.getValue());
                map.put("appid",jsonObject.get("app_id"));
            }
            if ("facebook".equals(c.getName())) {
                Map<String,Object> map = new HashMap<>();
                Map<String,Object> jsonObject = JSON.parseObject(c.getValue());
                map.put("appid",jsonObject.get("app_id"));
            }
            if ("user".equals(c.getName())) {
                result.put("user", JSON.parseObject(c.getValue()));
            }
        }
        return result;
    }
}
